﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using TemaAdministration.Models;

namespace TemaAdministration.Controllers
{ 
    public class DoctorController : Controller
    {
        private F11ITNET2Gr7Entities db = new F11ITNET2Gr7Entities();

        //
        // GET: /Doctor/

        public ViewResult Index()
        {
            return View(db.Doctors.ToList());
        }

        //
        // GET: /Doctor/Details/5

        public ViewResult Details(long id)
        {
            Doctor doctor = db.Doctors.Single(d => d.DocId == id);
            return View(doctor);
        }

        //
        // GET: /Doctor/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Doctor/Create

        [HttpPost]
        public ActionResult Create(Doctor doctor)
        {
            if (ModelState.IsValid)
            {
                db.Doctors.AddObject(doctor);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(doctor);
        }
        
        //
        // GET: /Doctor/Edit/5
 
        public ActionResult Edit(long id)
        {
            Doctor doctor = db.Doctors.Single(d => d.DocId == id);
            return View(doctor);
        }

        //
        // POST: /Doctor/Edit/5

        [HttpPost]
        public ActionResult Edit(Doctor doctor)
        {
            if (ModelState.IsValid)
            {
                db.Doctors.Attach(doctor);
                db.ObjectStateManager.ChangeObjectState(doctor, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(doctor);
        }

        //
        // GET: /Doctor/Delete/5
 
        public ActionResult Delete(long id)
        {
            Doctor doctor = db.Doctors.Single(d => d.DocId == id);
            return View(doctor);
        }

        //
        // POST: /Doctor/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {            
            Doctor doctor = db.Doctors.Single(d => d.DocId == id);
            db.Doctors.DeleteObject(doctor);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}